package com.ysd.lis.util.lab;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.lis.entity.PubEmployee;
import com.ysd.lis.entity.SysBasDictDetail;
import com.ysd.lis.entity.SysOrgDepartment;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.*;
import com.ysd.lis.request.LabMaininfoPageDto;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.Objects;

public class GetLabMainInfoQueryWapper {

    public static MPJLambdaWrapper<LabMaininfo> getLabMainInfoQueryWapper(LabMaininfoPageDto maininfoPageDto) {
        MPJLambdaWrapper<LabMaininfo> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(LabMaininfo.class);
        lambdaQueryWrapper.selectAs(LabRptGroup::getBusinessType, LabMaininfo::getBusinessType);
        lambdaQueryWrapper.selectAs(LabSampleType::getSampleTypeName, LabMaininfo::getSampleTypeNa);
        lambdaQueryWrapper.selectAs(SysOrgDepartment::getName, LabMaininfo::getReqDeptNa);
        lambdaQueryWrapper.selectAs("req", PubEmployee::getName, LabMaininfo::getReqDoctorNa);
        lambdaQueryWrapper.selectAs("test", SysUser::getRealname, LabMaininfo::getTestDoctorNa);
        lambdaQueryWrapper.selectAs("chk", SysUser::getRealname, LabMaininfo::getChkDoctorNa);
        lambdaQueryWrapper.selectAs("rechk", SysUser::getRealname, LabMaininfo::getReChkDoctorNa);
        lambdaQueryWrapper.selectAs("prt", SysUser::getRealname, LabMaininfo::getPrintDoctorNa);
        lambdaQueryWrapper.selectAs("sampl", SysUser::getRealname, LabMaininfo::getSamplingDoctorNa);
        lambdaQueryWrapper.selectAs("signer", SysUser::getRealname, LabMaininfo::getSignerDoctorNa);
        lambdaQueryWrapper.selectAs("patTypeNa", SysBasDictDetail::getName, LabMaininfo::getPatTypeNa);
        lambdaQueryWrapper.selectAs("sexNa", SysBasDictDetail::getName, LabMaininfo::getSexNa);
        lambdaQueryWrapper.selectAs("idTypeNa", SysBasDictDetail::getName, LabMaininfo::getIdTypeNa);
        lambdaQueryWrapper.selectAs("rptgrp", LabRptGroup::getRptGroupName, LabMaininfo::getRptGroupNa);
        lambdaQueryWrapper.leftJoin(LabRptGroup.class, LabRptGroup::getRptGroup, LabMaininfo::getRptGroup);
        lambdaQueryWrapper.leftJoin(LabSampleType.class, on -> on.eq(LabSampleType::getSampleTypeCode, LabMaininfo::getSampleType).eq(LabSampleType::getDelFlag, 0));
        lambdaQueryWrapper.leftJoin(SysOrgDepartment.class, on -> on.eq(SysOrgDepartment::getCode, LabMaininfo::getReqDept).eq(SysOrgDepartment::getDelFlag, 0));
        lambdaQueryWrapper.leftJoin(PubEmployee.class, "req", on -> on.eq(PubEmployee::getCode, LabMaininfo::getReqDoctor).eq(PubEmployee::getDelFlag, 0));
        lambdaQueryWrapper.leftJoin(SysUser.class, "test", SysUser::getUsername, LabMaininfo::getTestDoctor);
        lambdaQueryWrapper.leftJoin(SysUser.class, "chk", SysUser::getUsername, LabMaininfo::getChkDoctor);
        lambdaQueryWrapper.leftJoin(SysUser.class, "rechk", SysUser::getUsername, LabMaininfo::getReChkDoctor);
        lambdaQueryWrapper.leftJoin(SysUser.class, "prt", SysUser::getUsername, LabMaininfo::getPrintDoctor);
        lambdaQueryWrapper.leftJoin(SysUser.class, "sampl", SysUser::getUsername, LabMaininfo::getSamplingDoctor);
        lambdaQueryWrapper.leftJoin(SysUser.class, "signer", SysUser::getUsername, LabMaininfo::getSignerDoctor);
        lambdaQueryWrapper.leftJoin(SysBasDictDetail.class, "patTypeNa", on -> on.eq(SysBasDictDetail::getCode, LabMaininfo::getPatType).eq(SysBasDictDetail::getDelFlag,0));
        lambdaQueryWrapper.leftJoin(SysBasDictDetail.class, "sexNa", on -> on.eq(SysBasDictDetail::getCode, LabMaininfo::getSex).eq(SysBasDictDetail::getDelFlag,0));
        lambdaQueryWrapper.leftJoin(SysBasDictDetail.class, "idTypeNa", on -> on.eq(SysBasDictDetail::getCode, LabMaininfo::getIdType).eq(SysBasDictDetail::getDelFlag,0));
        lambdaQueryWrapper.leftJoin(LabRptGroup.class, "rptgrp", LabRptGroup::getRptGroup, LabMaininfo::getRptGroup);
        lambdaQueryWrapper.leftJoin(LabReqMainQuery.class, LabReqMainQuery::getReqNo, LabMaininfo::getReqNo);
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getPatId()), LabReqMain::getPatId, maininfoPageDto.getPatId());
        lambdaQueryWrapper.eq(LabMaininfo::getDelFlag, 0);
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getRptGroup()), LabMaininfo::getRptGroup, maininfoPageDto.getRptGroup());
        if (StringUtils.isNotBlank(maininfoPageDto.getSampledaBegin()) && StringUtils.isNotBlank(maininfoPageDto.getSampledaEnd())) {
            Timestamp startTime = Timestamp.valueOf(maininfoPageDto.getSampledaBegin());
            Timestamp endTime = Timestamp.valueOf(maininfoPageDto.getSampledaEnd());
            lambdaQueryWrapper.ge(LabMaininfo::getSampleda, startTime);
            lambdaQueryWrapper.le(LabMaininfo::getSampleda, endTime);
        }
        if (StringUtils.isNotBlank(maininfoPageDto.getReqNoS()) && StringUtils.isNotBlank(maininfoPageDto.getReqNoE())) {
            lambdaQueryWrapper.ge(LabMaininfo::getReqNo, maininfoPageDto.getReqNoS());
            lambdaQueryWrapper.le(LabMaininfo::getReqNo, maininfoPageDto.getReqNoE());
        }
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getReqNo()), LabMaininfo::getReqNo, maininfoPageDto.getReqNo());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getPatNo()), LabMaininfo::getPatNo, maininfoPageDto.getPatNo());


//        lambdaQueryWrapper.like(StringUtils.isNotBlank(maininfoPageDto.getPatNa()), LabMaininfo::getPatNa, maininfoPageDto.getPatNa());

        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(maininfoPageDto.getPatNa())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabMaininfo::getPatNa, maininfoPageDto.getPatNa()).or().like(LabMaininfo::getPatPyName, maininfoPageDto.getPatNa()));
        }

        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getSex()), LabMaininfo::getSex, maininfoPageDto.getSex());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getBedNo()), LabMaininfo::getBedNo, maininfoPageDto.getBedNo());
        if (StringUtils.isNotBlank(maininfoPageDto.getSamplingTime())) {
            Timestamp samplingStart = Timestamp.valueOf(maininfoPageDto.getSamplingTime() + " 00:00:00");
            Timestamp samplingEnd = Timestamp.valueOf(maininfoPageDto.getSamplingTime() + " 23:59:59");
            lambdaQueryWrapper.ge(LabMaininfo::getSamplingTime, samplingStart);
            lambdaQueryWrapper.le(LabMaininfo::getSamplingTime, samplingEnd);
        }
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getFeeType()), LabMaininfo::getFeeType, maininfoPageDto.getFeeType());
        lambdaQueryWrapper.eq(Objects.nonNull(maininfoPageDto.getIsChk()), LabMaininfo::getIsChk, maininfoPageDto.getIsChk());
        lambdaQueryWrapper.eq(Objects.nonNull(maininfoPageDto.getIsPrint()), LabMaininfo::getIsPrint, maininfoPageDto.getIsPrint());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getReqDept()), LabMaininfo::getReqDept, maininfoPageDto.getReqDept());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getReqDoctor()), LabMaininfo::getReqDoctor, maininfoPageDto.getReqDoctor());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getTestDoctor()), LabMaininfo::getTestDoctor, maininfoPageDto.getTestDoctor());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getChkDoctor()), LabMaininfo::getChkDoctor, maininfoPageDto.getChkDoctor());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getPatType()), LabMaininfo::getPatType, maininfoPageDto.getPatType());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getTraits()), LabMaininfo::getTraits, maininfoPageDto.getTraits());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(maininfoPageDto.getTestPurpose()), LabMaininfo::getTestPurpose, maininfoPageDto.getTestPurpose());
        //过滤无条码
        if (Objects.nonNull(maininfoPageDto.getIsFilterNoSpecCode()) && maininfoPageDto.getIsFilterNoSpecCode()) {
            lambdaQueryWrapper.isNotNull(LabMaininfo::getReqNo);
        }
        return lambdaQueryWrapper;
    }
}
